import { Controller, Get, Inject, Query } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import * as packageJson from '../../../package.json';
import { CoolTag, TagTypes } from '@cool-midway/core';
import { PluginService } from '../plugin/service/info';
import { SwaggerApi } from './swagger/swagger.api';

/**
 * 欢迎界面
 */
@Controller('/apidoc')
export class ApidocController {
  @Inject()
  ctx: Context;

  @Inject()
  pluginService: PluginService;

  @Inject()
  swaggerApi: SwaggerApi;

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Get('/swaggerApiDoc')
  async swaggerApiDoc(@Query('source') source = 'admin') {
    // 获取插件实例
    if (this.ctx.app.getEnv() !== 'prod') {
      const instance = await this.pluginService.getInstance('api-doc');
      if (source) await this.swaggerApi.initEps(source);
      const apiItem = this.swaggerApi.group[source] as SwaggerApiItem[];
      return await instance.generateApiDoc(
        apiItem,
        `${packageJson.name}-接口文档`
      );
    } else {
      return '生产环境不能的调用';
    }
  }
}
